<template>
    <a-modal :confirm-loading="modalOption.loading" :okText="modalOption.okText" @ok="modalOption.onOk"
             @cancel="modalOption.onCancel" :cancelText="modalOption.cancelText" :width="modalOption.width" :visible="visible"
             :title="modalOption.title">
        <a-spin tip="loading" :spinning="loading">
            <div class="container">
                <a-form-model ref="formModel" :label-col="formOption.labelCol" :wrapper-col="formOption.wrapperCol"
                              :layout="formOption.layout" :label-align="formOption.labelAlign" :hide-required-mark="formOption.hideRequiredMark"
                              :rules="formOption.rules" :model="formOption.formData">
                    <div class="table">
                        <a-card hoverable>
                            <div class="tool-bar">
                                <a-row>
                                    <a-col :span="6">
                                        <a-form-model-item label="标准类型" prop="standard">
                                            <a-select :disabled="disabledEdit" @change="standardChange" class="inputs" allowClear v-model="formOption.formData.standard" placeholder="标准类型">
                                                <a-select-option
                                                    v-for="(item,i) in ['国标','省标']"
                                                    :key="i"
                                                    :value="item">{{item}}</a-select-option>
                                            </a-select>
                                        </a-form-model-item>
                                    </a-col>
                                    <a-col :span="6">
                                        <a-form-model-item label="品名" prop="materialId">
                                            <a-select ref="Ainput" :disabled="disabledEdit" @change="materialIdChange" class="inputs" allowClear v-model="formOption.formData.materialId" placeholder="品名">
                                                <a-select-option
                                                    v-for="(item,i) in searchSource.materialNameList"
                                                    :key="i"
                                                    :value="item.id">{{item.name}}</a-select-option>
                                            </a-select>
                                        </a-form-model-item>
                                    </a-col>
                                    <a-col :span="6">
                                        <a-form-model-item label="批号" prop="batch">
                                            <a-input :disabled="disabledEdit" class="inputs" allowClear v-model="formOption.formData.batch" placeholder="批号"></a-input>
                                        </a-form-model-item>
                                    </a-col>
                                    <a-col :span="6">
                                        <a-form-model-item label="重量(kg)" prop="stockNumSys">
                                            <a-input :disabled="disabledEdit" class="inputs" allowClear v-model="formOption.formData.stockNumSys" placeholder="重量(kg)"></a-input>
                                        </a-form-model-item>
                                    </a-col>
                                </a-row>

                            </div>
                            <div class="tool-bar">
                                <a-row>
                                    <a-col :span="6">
                                        <a-form-model-item label="生产日期" prop="scTime">
                                            <a-date-picker
                                                :disabled="disabledEdit"
                                                valueFormat="YYYY-MM-DD"
                                                class="inputs"
                                                allowClear
                                                v-model="formOption.formData.scTime"
                                                placeholder="生产日期"
                                                :locale="locale"></a-date-picker>
                                        </a-form-model-item>
                                    </a-col>
                                    <a-col :span="6">
                                        <a-form-model-item label="复验期" prop="reCheckYp">
                                            <a-date-picker
                                                valueFormat="YYYY-MM-DD"
                                                class="inputs"
                                                allowClear
                                                v-model="formOption.formData.reCheckYp"
                                                placeholder="生产日期"
                                                :locale="locale"></a-date-picker>
                                        </a-form-model-item>
                                    </a-col>
                                    <a-col :span="6">
                                        <a-form-model-item label="质量状态" prop="qualityState">
                                            <a-select class="inputs" allowClear v-model="formOption.formData.qualityState" placeholder="质量状态">
                                                <a-select-option
                                                    v-for="(item,i) in ['合格','不合格']"
                                                    :key="i"
                                                    :value="item">{{item}}</a-select-option>
                                            </a-select>
                                        </a-form-model-item>
                                    </a-col>
                                </a-row>
                            </div>
                        </a-card>
                    </div>
                </a-form-model>
            </div>
            <div slot="footer" style="text-align: center">
                <a-button size="large" @click="modalOption.onCancel">取 消</a-button>
                <a-button size="large" type="primary" @click="modalOption.onOk">确 定</a-button>
            </div>
        </a-spin>
    </a-modal>
</template>
<script lang="ts">
import { Vue, Component, Prop, Watch } from "vue-property-decorator";
import locale from "ant-design-vue/lib/date-picker/locale/zh_CN";
import nextApi from "@/api/nextApi";
import areaJson, {getAreaWords} from "@/utils/area";

@Component({})
export default class extends Vue {
    @Prop({ default: false }) visible!: boolean;
    @Prop({ default: "" }) editId!: string;
    @Prop({ default: {} }) formData!: any;
    @Watch("visible")
    visibleChange(val) {
        if (val) {
            this.getMaterialNameList()
        }else{
            //关闭弹窗初始化数据组
            this.searchSource = {
                materialNameList:[]
            }
            this.formOption.formData = {}
        }
    }
    loading = false
    disabledEdit = false
    @Watch("editId",{immediate:true})
    editIdChange(val) {
        if (val) {
            console.log('id:',val)
            this.modalOption.title = `编辑`;
            this.disabledEdit = true
            this.getInfo()
        }else{
            this.modalOption.title = `新增`;
        }
    }
    //组件配置
    locale = locale;
    modalOption: any = {
        width: "90%",
        title: "新增",
        okText: "保存",
        cancelText: "取消",
        loading: false,
        onOk: () => {
            this.modalOk();
        },
        onCancel: () => {
            this.closeModal();
        },
    };

    //表单数据源
    searchSource:any = {
        materialNameList:[]
    }
    //表单配置
    formOption: any = {
        labelCol: { style: { width: "140px" } },
        wrapperCol: { style: {width:'calc(100% - 140px)'}},
        layout: "inline",
        labelAlign: "right",
        hideRequiredMark: false,
        formData: {
            materialId:'',//默认property否则清除后无法展示
        },
        rules: {
            standard: [{ required: true, message: "请选择标准类型" },],
            materialId: [{ required: true, message: "请选择品名" }],
            batch: [{ required: true, message: "请输入批号" }],
            stockNumSys: [{ required: true, message: "请输入重量"}],
            scTime: [{ required: true, message: "请选择生产日期"}],
            reCheckYp: [{ required: true, message: "请选择复验期"}],
            qualityState: [{ required: true, message: "请选择质量状态"}],
        },
    };
    materialIdChange(v){
        let find = this.searchSource.materialNameList.find(item=>item.id === v)
        if(find){
            this.formOption.formData.materialName = find.name
        }
    }
    /**
     * @description 查询一些数据
     * **/
    standardChange(){
        this.formOption.formData.materialId = ''
        this.getMaterialNameList()
    }
    async getMaterialNameList(){
        this.loading = true
        const res = await nextApi.selectMaterialListByType({materialType:"饮片",standard:this.formOption.formData.standard})
        this.searchSource.materialNameList = res.data
        this.loading = false
    }
    //查询详情后进行修改
    async getInfo(){
        this.loading = true
        this.formOption.formData = this.formData
        setTimeout(()=>{
            this.loading = false
        },400)
    }
    //提交
    modalOk(){
        (this.$refs.formModel as any).validate(async (valid: boolean) => {
            if (valid) {
                this.modalOption.loading = true;
                try {
                    if(this.editId){
                        console.log(this.formOption.formData,'修改')
                        const res = await nextApi.productStorageUpdate(this.formOption.formData)
                        this.$message.success(res.msg)
                        this.$emit("closeModal")
                    }else{
                        console.log(this.formOption.formData,'新增')
                        const res = await nextApi.rawProductStorageAdd(this.formOption.formData)
                        this.$message.success(res.msg)
                        this.$emit("closeModal")
                    }
                }finally {
                    this.modalOption.loading = false;
                }
            }
        });
    }

    closeModal() {
        this.disabledEdit = false
        this.$emit("closeModal");
    }
}
</script>
<style scoped lang="scss">
.container {
    padding: 0;
    .MR-10 {
        margin-right: 10px;
    }
    .inputs {
        width: 200px;
    }
    .inputs-with-qus{
        width: 173px;
    }
    .tool-bar {
        margin-bottom: 20px;

    }
    .table {
        margin-top: 20px;
    }
}
</style>
